package com.skivingcloud.admin.sys.service.impl;

import com.skivingcloud.admin.sys.entity.SysMsgDef;
import com.skivingcloud.admin.sys.mapper.SysMsgDefMapper;
import com.skivingcloud.admin.sys.service.ISysMsgDefService;
import com.skivingcloud.admin.utils.JacksonJsonUtil;
import com.skivingcloud.common.utils.PageUtils;
import com.skivingcloud.common.utils.StringUtils;
import com.skivingcloud.common.utils.UuidUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import net.sf.json.JSONObject;

/**
 * <p>
 * 消息规则定义 服务实现类
 * </p>
 *
 * @author skivingcloud
 * @since 2022-01-29
 */
@Service
public class SysMsgDefServiceImpl extends ServiceImpl<SysMsgDefMapper, SysMsgDef> implements ISysMsgDefService {

	/**
	 * 分页查询
	 * 
	 * @param params
	 * @return
	 * @throws IOException
	 * @throws JsonMappingException
	 * @throws JsonParseException
	 */
	@Override
	public PageUtils queryPage(Map<String, Object> params) throws Exception {
		PageUtils pageUtils = PageUtils.getPageInfo(params);
		Page<SysMsgDef> p = PageHelper.startPage(pageUtils.getPage(), pageUtils.getLimit());
		String jsonStr = JSONObject.fromObject(params.get("dataForm")).toString();
		SysMsgDef sysMsgDef = (SysMsgDef) JacksonJsonUtil.jsonToObj(new SysMsgDef(), jsonStr);
		QueryWrapper<SysMsgDef> queryWrapper = new QueryWrapper<SysMsgDef>();
		
		if (sysMsgDef != null) {
			if (StringUtils.isNotBlank(sysMsgDef.getMsgCode()))
				queryWrapper.like("msg_code", sysMsgDef.getMsgCode());
			if (StringUtils.isNotBlank(sysMsgDef.getPageType()))
				queryWrapper.in("page_type", Arrays.asList(sysMsgDef.getPageType().split(",")));
		}
		queryWrapper.orderByDesc("mod_time");
		List<SysMsgDef> list = this.baseMapper.selectList(queryWrapper);
		pageUtils = new PageUtils(p);
		return pageUtils;
	}

	/**
	 * 根据id获取消息规则信息
	 * 
	 * @param id
	 * @return
	 */
	@Override
	public SysMsgDef selectById(String id) throws Exception {
		return this.baseMapper.selectById(id);
	}

	/**
	 * 保存消息规则信息
	 * 
	 * @param sysMsgDef
	 */
	@Override
	public void saveMe(SysMsgDef sysMsgDef) {
		if (StringUtils.isBlank(sysMsgDef.getId()))
			sysMsgDef.setId(UuidUtil.get32UUID());
		this.baseMapper.insert(sysMsgDef);
	}

	/**
	 * 更新消息规则信息
	 * 
	 * @param sysMsgDef
	 */
	@Override
	public void updateMe(SysMsgDef sysMsgDef) {
		this.baseMapper.updateById(sysMsgDef);
	}

	/**
	 * 删除消息规则信息
	 * 
	 * @param ids
	 */
	@Override
	public void delete(String[] ids) {
		this.baseMapper.deleteBatchIds(Arrays.asList(ids));

	}

	/**
	 * 根据编号获取消息规则信息
	 * 
	 * @param code
	 * @return
	 */
	@Override
	public SysMsgDef selectSysMsgDefByCode(String code) {
		QueryWrapper<SysMsgDef> queryWrapper = new QueryWrapper<SysMsgDef>();
		queryWrapper.eq("msg_code", code);
		List<SysMsgDef> list = this.baseMapper.selectList(queryWrapper);
		return list.isEmpty() ? null : list.get(0);
	}

	/**
	 * 页面信息
	 * 
	 * @param id
	 * @return
	 */
	@Override
	public SysMsgDef pageInfo(String id) {
		return this.baseMapper.pageInfo(id);
	}
}
